package com.pengweiwei.androidzygote.start;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.RawRes;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.VideoView;

import com.putong35.common.baseactfrag.fragment.BaseFragment;
import com.pengweiwei.androidzygote.R;

import butterknife.BindView;
import butterknife.OnClick;


public class AnimationGuideFragment extends BaseFragment implements AnimationGuideActivity.PageSelectedListener {

    private @RawRes int videoRaw;
    private boolean isLastPage;

    @BindView(R.id.videoView)
    VideoView videoView;
    @BindView(R.id.ivPreview)
    ImageView ivPreview;
    @BindView(R.id.btnToMain)
    Button btnToMain;

    @OnClick(R.id.btnToMain)
    public void onBtnToMainClicked() {
        getActivity().setResult(Activity.RESULT_OK);
        getActivity().finish();
    }

    public static AnimationGuideFragment newInstance(@RawRes int videoRaw, boolean isLastPage) {
        Bundle args = new Bundle();
        args.putInt("videoRaw", videoRaw);
        args.putBoolean("isLastPage", isLastPage);
        AnimationGuideFragment fragment = new AnimationGuideFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public static AnimationGuideFragment newInstance(@RawRes int videoRaw) {
        return newInstance(videoRaw, false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.videoRaw = getArguments().getInt("videoRaw");
        this.isLastPage = getArguments().getBoolean("isLastPage");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        Uri uri = Uri.parse("android.resource://" + getActivity().getPackageName() + "/" + videoRaw);
        videoView.setOnPreparedListener(mp -> mp.setOnInfoListener((mp1, what, extra) -> {
            // 第一帧准备好了
            if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
                ivPreview.setVisibility(View.GONE);
                videoView.setBackgroundColor(Color.TRANSPARENT);
            }
            return true;
        }));
     /*   videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
                    @Override
                    public boolean onInfo(MediaPlayer mp, int what, int extra) {
                        if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START)
                            videoView.setBackgroundColor(Color.TRANSPARENT);
                        return true;
                    }
                });*/

        videoView.setVideoPath(uri.toString());
        AssetFileDescriptor afd = getResources().openRawResourceFd(videoRaw);
        if (afd != null) {
            MediaMetadataRetriever mmr = new MediaMetadataRetriever();
            mmr.setDataSource(getActivity(), uri);
            Bitmap bitmap = mmr.getFrameAtTime();
            ivPreview.setImageBitmap(bitmap);
            mmr.release();
        }

        if (isLastPage) {
            btnToMain.setVisibility(View.VISIBLE);
        }
        return view;
    }

    private void startAnimation() {
        videoView.start();
    }


    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_animation_guide, null);
    }

    @Override
    public void onPageSelected() {
        Log.e("pww", "onPageSelected: ");
        startAnimation();
    }

}
